package com.lzb.compressmaster.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.adapter.FrameRateAdapter;
import com.lzb.compressmaster.adapter.ResolutionAdapter;
import com.lzb.compressmaster.adapter.VideoCompressTypeAdapter;
import com.lzb.compressmaster.bean.FrameRateBean;
import com.lzb.compressmaster.bean.ResolutionBean;
import com.lzb.compressmaster.bean.VideoCompressTypeBean;
import com.lzb.compressmaster.databinding.ActivityVideoCompressBinding;
import com.lzb.compressmaster.dialog.RenameDialog;
import com.lzb.compressmaster.utils.VideoCompressor;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VideoCompressActivity extends BaseAc<ActivityVideoCompressBinding> implements View.OnClickListener, OnItemClickListener {

    public static String sVideoPath;
    private VideoCompressTypeAdapter typeAdapter;
    private ResolutionAdapter resolutionAdapter;
    private FrameRateAdapter frameRateAdapter;
    private VideoCompressor.CompressConfig compressConfig;
    private String newName;
    private int typePos, resolutionPos, frameRatePos;
    private String selResolution, selFrameRate;
    private int width, height, frameRate;

    @Override
    protected int onCreate() {
        return R.layout.activity_video_compress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVideoCompressBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvFileName.setOnClickListener(this);
        mDataBinding.tvStartCompress.setOnClickListener(this);

        mDataBinding.rvCompressType.setLayoutManager(new GridLayoutManager(mContext, 4));
        typeAdapter = new VideoCompressTypeAdapter();
        mDataBinding.rvCompressType.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(this);

        mDataBinding.rvResolutionList.setLayoutManager(new GridLayoutManager(mContext, 4));
        resolutionAdapter = new ResolutionAdapter();
        mDataBinding.rvResolutionList.setAdapter(resolutionAdapter);
        resolutionAdapter.setOnItemClickListener(this);

        mDataBinding.rvFrameRateList.setLayoutManager(new GridLayoutManager(mContext, 5));
        frameRateAdapter = new FrameRateAdapter();
        mDataBinding.rvFrameRateList.setAdapter(frameRateAdapter);
        frameRateAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvFileName) {//文件名
            showRenameDialog();
        } else if (id == R.id.tvStartCompress) {//开始压缩
            startCompress();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == typeAdapter) {
            typeAdapter.getItem(typePos).setSelected(false);
            typePos = position;
            typeAdapter.getItem(position).setSelected(true);
            typeAdapter.notifyDataSetChanged();
            compressConfig.setCompressType(typeAdapter.getItem(position).getCompressType());
            updateView();
        } else if (adapter == resolutionAdapter) {
            resolutionAdapter.getItem(resolutionPos).setSelected(false);
            resolutionPos = position;
            resolutionAdapter.getItem(position).setSelected(true);
            resolutionAdapter.notifyDataSetChanged();
            selResolution = resolutionAdapter.getItem(position).getName();
        } else if (adapter == frameRateAdapter) {
            frameRateAdapter.getItem(frameRatePos).setSelected(false);
            frameRatePos = position;
            frameRateAdapter.getItem(position).setSelected(true);
            frameRateAdapter.notifyDataSetChanged();
            selFrameRate = frameRateAdapter.getItem(position).getName();
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(sVideoPath).into(mDataBinding.ivImg);
        newName = FileUtils.getFileNameNoExtension(sVideoPath);
        mDataBinding.tvFileName.setText(newName);
        mDataBinding.tvFileSize.setText(getString(R.string.file_size_text) + ConvertUtils.byte2FitMemorySize(new File(sVideoPath).length(), 2));
        VideoCompressor.VideoInfo videoInfo = VideoCompressor.getVideoInfo(sVideoPath);
        mDataBinding.tvResolution.setText(getString(R.string.resolution_text) + videoInfo.getWidth() + "*" + videoInfo.getHeight());

        List<VideoCompressTypeBean> listType = new ArrayList<>();
        listType.add(new VideoCompressTypeBean(VideoCompressor.CompressType.SMART, true));
        listType.add(new VideoCompressTypeBean(VideoCompressor.CompressType.QUALITY_FIRST, false));
        listType.add(new VideoCompressTypeBean(VideoCompressor.CompressType.SIZE_FIRST, false));
        listType.add(new VideoCompressTypeBean(VideoCompressor.CompressType.CUSTOM, false));
        typeAdapter.setList(listType);

        List<ResolutionBean> listResolution = new ArrayList<>();
        listResolution.add(new ResolutionBean(getString(R.string.resolution_text1), true));
        listResolution.add(new ResolutionBean(getString(R.string.resolution_text2), false));
        listResolution.add(new ResolutionBean(getString(R.string.resolution_text3), false));
        listResolution.add(new ResolutionBean(getString(R.string.resolution_text4), false));
        resolutionAdapter.setList(listResolution);

        List<FrameRateBean> listFrame = new ArrayList<>();
        listFrame.add(new FrameRateBean(getString(R.string.frame_rate_text1), true));
        listFrame.add(new FrameRateBean(getString(R.string.frame_rate_text2), false));
        listFrame.add(new FrameRateBean(getString(R.string.frame_rate_text3), false));
        listFrame.add(new FrameRateBean(getString(R.string.frame_rate_text4), false));
        listFrame.add(new FrameRateBean(getString(R.string.frame_rate_text5), false));
        frameRateAdapter.setList(listFrame);

        compressConfig = VideoCompressor.createSmartConfig();
        selResolution = getString(R.string.resolution_text1);
        selFrameRate = getString(R.string.frame_rate_text1);
    }

    private void startCompress() {
        String path = PathUtils.getFilesPathExternalFirst() + Content.myVideoCompress + "/" + newName + "." + FileUtils.getFileExtension(sVideoPath);
        getResolution();
        getFrame();
        compressConfig.setWidth(width);
        compressConfig.setHeight(height);
        compressConfig.setFrameRate(frameRate);
        showDialog(getString(R.string.compress_ing));
        VideoCompressor.compressVideo(sVideoPath, path, compressConfig, new VideoCompressor.CompressCallback() {
            @Override
            public void onComplete(VideoCompressor.CompressResult result) {
                dismissDialog();
                VideoCompressSucActivity.result = result;
                startActivity(VideoCompressSucActivity.class);
            }

            @Override
            public void onError(String error) {
                dismissDialog();
                ToastUtils.showShort(error);
            }
        });
    }

    private void getResolution() {
        if (selResolution.equals(getString(R.string.resolution_text1))) {
            width = 0;
            height = 0;
        } else if (selResolution.equals(getString(R.string.resolution_text2))) {
            width = 854;
            height = 480;
        } else if (selResolution.equals(getString(R.string.resolution_text3))) {
            width = 1280;
            height = 720;
        } else if (selResolution.equals(getString(R.string.resolution_text4))) {
            width = 1920;
            height = 1080;
        }
    }

    private void getFrame() {
        if (selFrameRate.equals(getString(R.string.frame_rate_text1))) {
            frameRate = 0;
        } else if (selFrameRate.equals(getString(R.string.frame_rate_text2))) {
            frameRate = 18;
        } else if (selFrameRate.equals(getString(R.string.frame_rate_text3))) {
            frameRate = 24;
        } else if (selFrameRate.equals(getString(R.string.frame_rate_text4))) {
            frameRate = 30;
        } else if (selFrameRate.equals(getString(R.string.frame_rate_text5))) {
            frameRate = 50;
        }
    }

    private void updateView() {
        mDataBinding.llCustom.setVisibility(View.GONE);
        if (compressConfig.getCompressType() == VideoCompressor.CompressType.CUSTOM) {
            mDataBinding.llCustom.setVisibility(View.VISIBLE);
        }
    }

    private void showRenameDialog() {
        RenameDialog renameDialog = new RenameDialog(mContext);
        renameDialog.setName(newName);
        renameDialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                newName = name;
                mDataBinding.tvFileName.setText(newName);
            }
        });
        renameDialog.show();
    }

}